package com.czk.doc;

import com.alibaba.fastjson.JSON;
import com.czk.entity.User;
import com.czk.util.ClientUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * 批量增加
 *
 * @Author:ChenZhangKun
 * @Date: 2021/6/13 23:31
 */
public class BenchInsert {
    public static void main(String[] args) throws IOException {
        // 客户端
        RestHighLevelClient highLevelClient = ClientUtil.getHighLevelClient();
        //
        BulkRequest bulkRequest = new BulkRequest();
        for (int i = 0; i < 5; i++) {
            bulkRequest.add(new IndexRequest().index("user").id((i + 1) + "").source(JSON.toJSONString(new User("czk", 26, "man")), XContentType.JSON));
        }
        BulkResponse response = highLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        // 花费得时间
        TimeValue took = response.getTook();
        // 时间
        System.out.println(took);
        System.out.println(response.getItems());
        highLevelClient.close();
    }
}
